package com.sgb.kjwl.view.ui.adapter.search;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemRecommendGoodsHomeBinding;
import com.sgb.kjwl.model.entity.user.RecommendListEntity;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.view.ui.adapter.enterprise.TextViewBindingAdapter;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * @author Administrator
 */
public class RecommendGoodsAdapter extends BaseRecycleAdapter<RecommendListEntity> {

    private ItemRecommendGoodsHomeBinding mBinding;
    private ResembleGoodsClick mResembleGoodsClick;

    public void setmResembleGoodsClick(ResembleGoodsClick mResembleGoodsClick) {
        this.mResembleGoodsClick = mResembleGoodsClick;
    }

    public RecommendGoodsAdapter(Context context, List<RecommendListEntity> datas) {
        super(context, R.layout.item_recommend_goods_home, datas);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ItemViewHolder holder, final RecommendListEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        mBinding = (ItemRecommendGoodsHomeBinding) holder.getmBinding();
        RelativeLayout mRlRecommendMasking = holder.getItem().findViewById(R.id.rl_recommend_masking);
        ImageView logoView = holder.getItem().findViewById(R.id.logo_view);
        ImageView ziYingView = holder.getItem().findViewById(R.id.image_ziying);
        if ("1".equals(entity.getSelfSupportTag())) {
            ziYingView.setVisibility(View.VISIBLE);
        } else {
            ziYingView.setVisibility(View.INVISIBLE);
        }
        TextView tvPrice = holder.getItem().findViewById(R.id.tv_price);
        TextView tvStorageName = holder.getItem().findViewById(R.id.tv_storage_name);
        if (!TextUtils.isEmpty(entity.getVipPrice()) && entity.getPromoteStatus() == 2) {
            if (Double.valueOf(entity.getVipPrice())>Double.valueOf(entity.getPromotePrice())) {
                holder.setVisible(R.id.tag_cuxiao, true);
                tvPrice.setText("¥" + TextViewBindingAdapter.setMoney(entity.getPromotePrice()) + "/" + entity.getUnit());
                tvPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
                holder.setVisible(R.id.tag_vip, false);
            }else {
                holder.setVisible(R.id.tag_cuxiao, true);
                tvPrice.setText("¥" + TextViewBindingAdapter.setMoney(entity.getVipPrice()) + "/" + entity.getUnit());
                tvPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                holder.setVisible(R.id.tag_vip, true);
            }
        } else if (!TextUtils.isEmpty(entity.getVipPrice())) {
            holder.setVisible(R.id.tag_cuxiao, false);
            tvPrice.setText("¥" + TextViewBindingAdapter.setMoney(entity.getVipPrice()) + "/" + entity.getUnit());
            tvPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
            holder.setVisible(R.id.tag_vip, true);
        } else if (entity.getPromoteStatus() == 2){
            holder.setVisible(R.id.tag_cuxiao, true);
            tvPrice.setText("¥" + TextViewBindingAdapter.setMoney(entity.getPromotePrice()) + "/" + entity.getUnit());
            tvPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
            holder.setVisible(R.id.tag_vip, false);
        }else {
            holder.setVisible(R.id.tag_cuxiao, false);
            tvPrice.setText("¥" + TextViewBindingAdapter.setMoney(PointUtils.price(Double.valueOf(entity.getPrice()))) + "/" + entity.getUnit());
            tvPrice.setTextColor(context.getResources().getColor(R.color.new_org));
            holder.setVisible(R.id.tag_vip, false);
        }
        mRlRecommendMasking.setVisibility(View.GONE);
        mBinding.ivMasking.setOnClickListener(v -> {
                    mRlRecommendMasking.setVisibility(View.VISIBLE);
                }
        );
        tvStorageName.setText("仓储:" + entity.getStorageName());
        mBinding.rlRecommendMasking.setOnClickListener(v -> {
                    mRlRecommendMasking.setVisibility(View.GONE);
                }
        );

        holder.itemView.setOnLongClickListener(v -> {
            mRlRecommendMasking.setVisibility(View.VISIBLE);
            return true;
        });

        mBinding.tvResembleGoods.setOnClickListener(v -> {
            if (mResembleGoodsClick != null) {
                mResembleGoodsClick.resembleGoodsClick(entity);
            }
        });
        if (LocationUtil.isOpenLocation(context) && LocationUtil.currentLocationIsValid()) {
            mBinding.viewDistance.setVisibility(View.VISIBLE);
        } else {
            mBinding.viewDistance.setVisibility(View.GONE);
        }

        Glide.with(context).load(entity.getImgUrl()).apply(new RequestOptions().placeholder(R.drawable.null_img_list)
                .error(R.drawable.null_img_list)
        ).into(logoView);
    }

    public interface ResembleGoodsClick {
        void resembleGoodsClick(RecommendListEntity recommendListEntity);
    }
}
